package com.every.service.impl;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.every.entity.WechatMessage;
import com.every.mapper.WechatMessageMapper;
import com.every.service.WechatMessageService;
import org.springframework.stereotype.Service;

/**
 * @description: 微信推送消息业务类
 * @author: yh
 * @date: 2022/11/9
 */
@Service
public class WechatMessageServiceImpl extends ServiceImpl<WechatMessageMapper, WechatMessage>  implements WechatMessageService {
    /**
     * 插入微信消息发送记录
     * @param templateId 模板id
     * @param openId 微信用户openid
     * @param content 发送内容
     * @author: yh
     * @date: 2022/11/9
     * @return 主键id
     */
    @Override
    public Long saveWechatMessage(String templateId, String openId, String content){
        WechatMessage message = new WechatMessage();
        message.setTemplateId(templateId);
        message.setOpenId(openId);
        message.setContent(content);
        baseMapper.insert(message);
        return message.getMessageId();
    }

    /**
     * 更细消息状态
     * @param messageId 微信消息主键id
     * @author: yh
     * @date: 2022/11/9
     */
    @Override
    public void updateState(Long messageId){
        LambdaUpdateChainWrapper<WechatMessage> wrapper = new LambdaUpdateChainWrapper<>(baseMapper);
        wrapper.eq(WechatMessage::getMessageId, messageId)
                .set(WechatMessage::getState, true)
                .update();
    }
}
